package Ventanas;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;

import ClasesBasicas.Cuestionario;
import Gestores.SistemaCuestionario;
import Gestores.SistemaPizzaSugerida;

import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

public class ventanaConsultarCuestionario extends JFrame {

	private JPanel contentPane;
	private JComboBox comboBox;
	private JTextField mediatextField;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ventanaConsultarCuestionario frame = new ventanaConsultarCuestionario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public ventanaConsultarCuestionario() throws ClassNotFoundException, SQLException {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage("src\\Fotos\\txoPizzeria.jpg"));
		setTitle("Consulta cuestionario");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 465, 282);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		comboBox = new JComboBox();
		SistemaCuestionario sp=new SistemaCuestionario();
		ArrayList<String> preguntas=sp.userList();
		int i=0;
		if(!preguntas.isEmpty()){
			
			while(i<preguntas.size()){
				comboBox.addItem(preguntas.get(i));
				i++;
			}
		}
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SistemaCuestionario sc=new SistemaCuestionario();
				Cuestionario pr;
				try {
					pr = sc.getPregunta(comboBox.getSelectedItem().toString());
					double media=(double)pr.getPuntuacionTotal()/pr.getVecesPuntuado();
					double mediaRedon=Math.rint(media*100)/100;
					mediatextField.setText(Double.toString(mediaRedon));
					textField.setText(Integer.toString(pr.getVecesPuntuado()));
				} catch (ClassNotFoundException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		comboBox.setBackground(Color.WHITE);
		comboBox.setFont(new Font("Tahoma", Font.PLAIN, 11));
		comboBox.setBounds(169, 46, 255, 20);
		contentPane.add(comboBox);
		
		JLabel lblSeleccioanarPregunta = new JLabel("Seleccionar Pregunta:");
		lblSeleccioanarPregunta.setBounds(10, 49, 130, 14);
		contentPane.add(lblSeleccioanarPregunta);
		
		JLabel lblPuntuacionMedia = new JLabel("Puntuacion media:");
		lblPuntuacionMedia.setBounds(118, 131, 116, 14);
		contentPane.add(lblPuntuacionMedia);
		
		mediatextField = new JTextField();
		mediatextField.setEditable(false);
		mediatextField.setBounds(240, 128, 142, 20);
		contentPane.add(mediatextField);
		mediatextField.setColumns(10);
		
		JLabel lblVecesPuntuada = new JLabel("Veces Puntuada:");
		lblVecesPuntuada.setBounds(118, 170, 102, 14);
		contentPane.add(lblVecesPuntuada);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(240, 167, 142, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnAtras = new JButton("Atras");
		btnAtras.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				ventanaAdminLogeado v=new ventanaAdminLogeado();
				v.setLocationRelativeTo(null);
				v.setVisible(true);
			}
		});
		btnAtras.setBounds(10, 205, 89, 23);
		contentPane.add(btnAtras);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon("src\\Fotos\\fondo-textured-blanco-y-azul-abstracto-5013168.jpg"));
		label.setBounds(-202, -133, 661, 386);
		contentPane.add(label);
		
		
		
		
	}
}
